/**  
 * All rights Reserved, Designed By <a href="www.itttq.com" target="_blank">www.itttq.com</a>
 * @date:   2018-01-6 11:42:21
 * @Copyright: 2017 <a href="www.itttq.com" target="_blank">www.itttq.com</a> Inc. All rights reserved. 
 */
package com.itttq.gshop.admin.controller.user;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import com.itttq.gshop.admin.controller.GenericController;
import com.itttq.gshop.domain.to.PageParamTo;
import com.itttq.gshop.domain.to.ResultPageTo;
import com.itttq.gshop.domain.po.UserAddressPo;
import com.itttq.gshop.generic.connector.IUserAddressConnector;

@RestController
@RequestMapping("user/address")
public class UserAddressController extends GenericController {
	private static final long serialVersionUID = 848162096667395214L;
	@Autowired
	private IUserAddressConnector userAddressConnector;
	
	/**
	 * 根据条件查询UserAddressPo对象，并分页
	 * 
	 * @param userAddress
	 * @param request
	 * @return
	 * @Author 迷蝶
	 * @return ModelAndView
	 */
	@RequestMapping("addresss.html")
	public ModelAndView getUserAddresss(UserAddressPo userAddress, HttpServletRequest request) {
		ModelAndView mv = new ModelAndView("/user/addresss");
		PageParamTo ppt = null == request ? new PageParamTo() : getPageParam(request);
		userAddress.setStart(ppt.getStart());
		userAddress.setSize(ppt.getSize());
		ResultPageTo<UserAddressPo> rpt = userAddressConnector.getUserAddresss(userAddress);
		this.getPageMv("addresss", mv, rpt);
		mv.addObject("address", userAddress);
		return mv;
	}

	
	/**
	 * 跳转到新增页面
	 * @return
	 */
	@RequestMapping("add.html")
	public ModelAndView addUserAddress() {
		ModelAndView mv = new ModelAndView("/user/addressAdd");
		
		return mv;
	}
	/**
	 * 跳转到查看页面
	 * @param id
	 * @return
	 */
	@RequestMapping("view/{id}.html")
	public ModelAndView viewUserAddress(@PathVariable("id") Integer id) {
		ModelAndView mv = addUserAddress();
		if(id != null) {
			mv.addObject("address", userAddressConnector.getUserAddress(id));
		}
		mv.addObject("op", "view");
		return mv;
	}
	/**
	 * 跳转到编辑页面
	 * @param id
	 * @return
	 */
	@RequestMapping("edit/{id}.html")
	public ModelAndView editUserAddress(@PathVariable("id") Integer id) {
		ModelAndView mv = viewUserAddress(id);
		mv.addObject("op", "edit");
		return mv;
	}
	
	/**
	 * 保存UserAddressPo对象，无ID则新增，有ID则更新
	 * 
	 * @param userAddress
	 * @return
	 * @Author 迷蝶
	 * @return ModelAndView
	 */
	@RequestMapping("save.html")
	public ModelAndView saveUserAddress(UserAddressPo userAddress) {
		userAddressConnector.saveUserAddress(userAddress);
		return getUserAddresss(new UserAddressPo(), null);
	}

	/**
	 * 根据ID删除UserAddressPo对象
	 * 
	 * @param id
	 * @return
	 * @Author 迷蝶
	 * @return ModelAndView
	 */
	@RequestMapping("del/{id}.html")
	@ResponseBody
	public ModelAndView delUserAddress(@PathVariable("id") Integer id) {
		if(id != null) {
			userAddressConnector.delUserAddress(id);
		}
		return getUserAddresss(new UserAddressPo(), null);
	}
}